package Action;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import Aptech.Common.Formater;
import Aptech.Service.SV_Contract;
import Aptech.Service.SV_Order;
import Form.BuiltsForm;
import Form.OrderForm;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Orders;

public class OrderAction extends DispatchAction{
	
		private	OrderForm orderForm;
		private SV_Order mgOrder = new SV_Order();
		private SV_Contract mgContract = new SV_Contract();
		
	public ActionForward getAllOder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
		orderForm = (OrderForm) form;
		orderForm.setArrOrder(mgOrder.getAllOder());
        request.setAttribute("sitemap", "List of Order");

        return mapping.findForward("list");
    }

        public ActionForward editOrder(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        	
        	orderForm = (OrderForm) form;
        	String id = request.getParameter("id");
        	request.setAttribute("sitemap", "Edit A Order");
        	
        	String type = request.getParameter("type");
        	if (type.equals("edit1")) {
                request.setAttribute("id", id);
                request.setAttribute("type", type);
                
                // Set value for Form
                Orders order = mgOrder.getOrderByID(id);
                
                orderForm.setIdContract(order.getIdContract());
                orderForm.setDate(Formater.date2str(order.getDate()));
                orderForm.setNameContract(mgContract.getContractByID(order.getIdContract()).getContractNumber());
                orderForm.setNote(order.getNote());
                if(order.getStatus().equals("Yet to be Registered")){
                	orderForm.setStatus(Boolean.TRUE);
                }else{
                	orderForm.setStatus(Boolean.FALSE);
                }
                
                orderForm.setStatusMoney(order.getStatusMoney());           
                orderForm.setId(id);              
            } else {            	
            	Orders order = new Orders();                
            	order.setIdContract(orderForm.getIdContract());
            	order.setId(orderForm.getId());              
            	order.setDate(Formater.str2SQLdate(orderForm.getDate()));
            	if(orderForm.getStatus() == Boolean.TRUE){
            		order.setStatus("Yet to be Registered");
            	}else{
            		order.setStatus("Payment not received");
            	}
            	            
            	order.setStatusMoney(orderForm.getStatusMoney());
            	order.setNote(orderForm.getNote());
            	mgOrder.editOrder(order);
            	request.setAttribute("save", "save");
            }


            return map.findForward("edit");
    }
        public ActionForward addOrder(ActionMapping map, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        	
        	orderForm = (OrderForm) form;
        	
            String type = request.getParameter("type");
            request.setAttribute("sitemap", "Add A New Order");
            if (type == null) {
                request.setAttribute("type", "add");
                return map.findForward("edit");
            } else {
                UUID id = UUID.randomUUID();
                
                Orders order = new Orders();                
            	order.setIdContract(orderForm.getIdContract());
            	order.setId(id.toString());              
            	order.setDate(Formater.str2SQLdate(orderForm.getDate()));
            	if(orderForm.getStatus() == Boolean.TRUE){
            		order.setStatus("Yet to be Registered");
            	}else{
            		order.setStatus("Payment not received");
            	}          
            	order.setStatusMoney(orderForm.getStatusMoney());
            	order.setNote(orderForm.getNote());               
            	mgOrder.addOrder(order);
            	request.setAttribute("save", "save");
            }

            return map.findForward("edit");
        }
        
        public ActionForward deleteOrder(ActionMapping map, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        	
        	String id = request.getParameter("id");
        	mgOrder.deleteOrder(id);
            response.sendRedirect("/NTB/order.do?method=getAllOder");
            
        	return map.findForward("list");
        }
        
        public ActionForward detailOrder(ActionMapping map, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        	
        	orderForm = (OrderForm) form;
            String id = request.getParameter("id");
            request.setAttribute("id", id);
            request.setAttribute("type", "detail");
            
            request.setAttribute("sitemap", "Information Detail of A Order");
            // Set value for Formpe
            Orders order = mgOrder.getOrderByID(id);            
            orderForm.setIdContract(order.getIdContract());
            orderForm.setNameContract(mgContract.getContractByID(order.getIdContract()).getContractNumber());
            orderForm.setDate(Formater.date2str(order.getDate()));
            orderForm.setNote(order.getNote());
            if(order.getStatus().equals("Yet to be Registered")){
            	orderForm.setStatus(Boolean.TRUE);
            }else{
            	orderForm.setStatus(Boolean.FALSE);
            }
            orderForm.setStatusMoney(order.getStatusMoney());           
            orderForm.setId(id);
                
        	return map.findForward("edit");
        }
}
